package main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class SaveLoadGame {
	
	public static String filename="savedGame";
	public static String originalProblem="";
	public static String solution="";
	public static String userSolution="";
	
	/**
	 * Save the game to a file.
	 * @param origProb, the original sudoku problem
	 * @param solution, the solution of the sudoku problem
	 * @param userInput, the user inputs
	 * @throws IOException
	 */
	public static void save(String origProb, String solution, String userInput) throws IOException
	{
	
		originalProblem=origProb;
		userSolution=userInput;
		File file = new File(filename);
		FileWriter fw = new FileWriter(file);
		BufferedWriter out = new BufferedWriter(fw);
		out.write(origProb);
		out.write("\n");
		out.write(solution);
		out.write("\n");
		out.write(userInput);
		out.flush();
		out.close();
	}
	
	/**
	 * Loads a saved game.
	 * @throws IOException
	 */
	public static void load() throws IOException
	{
		FileInputStream fstream = new FileInputStream(new File(filename));
		DataInputStream input = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(input));
		originalProblem = br.readLine();
		solution = br.readLine();
		userSolution = br.readLine();
		br.close();
	}
	
	/**
	 * Remove a saved game from the saving file.
	 * @throws IOException
	 */
	public static void removeSavedGame() throws IOException
	{
		BufferedWriter remove = new BufferedWriter(
				new FileWriter(
						new File(filename)
						)
				);
		remove.write(" ");
		remove.close();
		
	}
	
	/**
	 * Check if there is a saved game.
	 * @return true if it is, else false.
	 */
	public static boolean isGameSaved()
	{
		File file = new File(filename);
		if(file.exists())
		{
			if(file.length()<243)
				return false;
			else
				return true;
		}
		else
			return false;
	}


}
